package com.xyoto.takeawayBusiness.service.impl;

import com.xyoto.takeawayBusiness.constant.DataBasesOperationResult;
import com.xyoto.takeawayBusiness.dto.MenuTypeDTO;
import com.xyoto.takeawayBusiness.dto.UpdateTypeDTO;
import com.xyoto.takeawayBusiness.entity.ShoppingType;
import com.xyoto.takeawayBusiness.entity.UserCache;
import com.xyoto.takeawayBusiness.enums.CommonEnum;
import com.xyoto.takeawayBusiness.enums.MenuEnum;
import com.xyoto.takeawayBusiness.enums.UserShopEnum;
import com.xyoto.takeawayBusiness.exception.ExceptionUtils;
import com.xyoto.takeawayBusiness.mapper.*;
import com.xyoto.takeawayBusiness.service.ShoppingTypeService;
import com.xyoto.takeawayBusiness.utils.RedisUtils;
import com.xyoto.takeawayBusiness.utils.Result;
import com.xyoto.takeawayBusiness.vo.ShoppingFrontVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * @Description
 * @Author: TingFeng
 * @CreateTime: 2022/4/26  19:28
 */
@Slf4j
@Service
public class ShoppingTypeServiceImpl implements ShoppingTypeService {
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private HttpServletRequest request;
    @Resource
    private UserShopMapper userShopMapper;
    @Resource
    private ShoppingTypeMapper shoppingTypeMapper;
    @Resource
    private ShoppingGoodsMapper shoppingGoodsMapper;
    @Resource
    private ShoppingAttributeMapper shoppingAttributeMapper;
    @Resource
    private ShoppingMapper shoppingMapper;



    /**
     * 新增菜单分类
     * @param menuTypeDTO
     * @return
     */
    @Override
    public Result addType(MenuTypeDTO menuTypeDTO) {
        Integer shopId =findThisUserShopId();
        //没有权限
        if(!shopId.equals(menuTypeDTO.getShopId())){
            log.info("您没有权限");
            ExceptionUtils.error(CommonEnum.ILLEGAL_REQUEST);
        }
        log.info("获取商品分类最大order");
//        short typeCount=shoppingTypeMapper.getTypeCount(shopId);
        short typeCount = shoppingTypeMapper.findOrderMax(shopId);
        //加一代表排序最后一位
        typeCount+=1;
        ShoppingType shoppingType = new ShoppingType();
        shoppingType.setShopId(menuTypeDTO.getShopId());
        shoppingType.setTypeName(menuTypeDTO.getTypeName());
        shoppingType.setOrder(typeCount);
        //进行操作添加
        Integer add = shoppingTypeMapper.addType(shoppingType);
        log.info(add.toString());
        if(add==DataBasesOperationResult.ERROR){
            log.info("添加失败");
            ExceptionUtils.error(MenuEnum.MENU_ADD_ERROR);
        }
        log.info("添加分类成功");
        return Result.success();
    }

    /**
     * 获取菜单分类
     * @return
     */
    @Override
    public Result getMenu(Integer shopId) {

        Integer thisShopId =findThisUserShopId();
        //没有权限
        if(!shopId.equals(thisShopId)){
            log.info("没有权限");
            ExceptionUtils.error(CommonEnum.ILLEGAL_REQUEST);
        }
        log.info("开始查询菜单分类");
        List<ShoppingType> menu = userShopMapper.getMenu(shopId);
        log.info("查询成功");
        return Result.success(menu);
    }

    /**
     * 修改菜单分类
     * @param updateTypeDTO
     * @return
     */
    @Override
    public Result updateType(UpdateTypeDTO updateTypeDTO) {
        Integer shopId =findThisUserShopId();

        log.info("开始查询本菜单所属店铺");
        Integer thisShopId = shoppingTypeMapper.findShopIdByShopTypeId(updateTypeDTO.getId());
        //没有权限
        if(!thisShopId.equals(shopId)){
            ExceptionUtils.error(CommonEnum.ILLEGAL_REQUEST);
        }
        //有权限修改
        ShoppingType shoppingType = new ShoppingType();
        BeanUtils.copyProperties(updateTypeDTO,shoppingType);
        Integer update = shoppingTypeMapper.updateType(shoppingType);
        if(update == DataBasesOperationResult.ERROR){
            log.info("修改失败");
            ExceptionUtils.error(MenuEnum.MeNU_UPDATE_ERROR);
        }
        log.info("修改成功");
        return Result.success();
    }

    /**
     * 删除菜单分类
     * @param menuId
     * @return
     */
    @Override
    @Transactional
    public Result removeType(Integer menuId) {

        Integer shopId =findThisUserShopId();

        log.info("开始查询本菜单所属店铺");
        Integer thisShopId = shoppingTypeMapper.findShopIdByShopTypeId(menuId);
        //没有权限
        if(thisShopId==null||!thisShopId.equals(shopId)){
            log.info("没有权限");
            ExceptionUtils.error(CommonEnum.ILLEGAL_REQUEST);
        }
        log.info("开始删除菜单分类");
        deleteTypeGoods(menuId);
        Integer deleteType = shoppingTypeMapper.deleteType(menuId);
        if(deleteType == DataBasesOperationResult.ERROR){
            log.info("删除此菜单分类失败");
            ExceptionUtils.error(MenuEnum.MENU_DELETE_ERROR);
        }
        log.info("删除菜单分类成功");
        return Result.success();
    }

    /**
     * 获取商品页
     * @param shopId
     * @return
     */
    @Override
    public Result getShop(Integer shopId) {
        Integer thisUserShopId = findThisUserShopId();
        //没有权限
        if(!shopId.equals(thisUserShopId)){
            log.info("没有权限");
            ExceptionUtils.error(CommonEnum.ILLEGAL_REQUEST);
        }
        log.info("开始获取商品页");
        log.info("开始获取菜单分类");
        List<ShoppingType> menu = userShopMapper.getMenu(shopId);
        HashMap<String, Object> shopMap = new HashMap<>();
        for (ShoppingType shoppingType : menu) {
            String shopTypeName = shoppingType.getTypeName();
            Integer typeId = shoppingType.getId();
            log.info("开始查询"+shopTypeName+"下的商品");
            List<ShoppingFrontVO> shoppingList =userShopMapper.findShoppingById(typeId,shopId);
            shopMap.put(shopTypeName,shoppingList);
        }
        log.info("查询成功");
        return Result.success(shopMap);
    }

    /**
     * 批量删除菜单分类下商品所有东西
     */
    public void deleteTypeGoods(Integer menuId){
        log.info("查询此菜单分类下的所有商品id");
        List<Integer> shoppingIdList =
                shoppingMapper.findShoppingIdBymenuId(menuId);
        for (Integer shoppingId : shoppingIdList) {
            log.info("开始删除"+shoppingId+"商品");
//            "开始删除当前商品的所有商品规格"
            int deleteGoodsCount =
                    shoppingGoodsMapper.deleteGoodsByShoppingId(shoppingId);
//            "开始删除当前商品的所有商品属性"
            int deleteAttributeCount =
                    shoppingAttributeMapper.deleteAttributeByShoppingId(shoppingId);
//            "开始删除本商品"
            Integer deleteShopping = shoppingMapper.deleteShopping(shoppingId);
            if(deleteShopping == DataBasesOperationResult.ERROR){
                log.info("删除此菜单分类下的商品失败");
                ExceptionUtils.error(MenuEnum.MENU_DELETE_ERROR);
            }
        }
        log.info("删除菜单分类下所有成功");
    }

    /**
     * 查询该用户拥有的店铺id
     * @return
     */
    public Integer findThisUserShopId(){
        //获取用户信息
        UserCache redisUserMessage = redisUtils.getRedisUserMessage(request);
        Integer UserShopId = redisUserMessage.getId();
        //查询用户是否有权限操作
        Integer thisShopId = userShopMapper.findShopIdByUserId(UserShopId);
        if(thisShopId==null) {
            log.info("查询为空");
            ExceptionUtils.error(UserShopEnum.FIND_USER_SHOP_ERROR);
        }
        return thisShopId;
    }
}
